//
//  CZStatusPictureView.swift
//  Weibo27
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// cell 重用标识
private let CZStatusPictureViewCellId = "CZStatusPictureViewCellId"

// cell 间距
private let itemMargin: CGFloat = 5
// cell 的宽高
private let cellWH: CGFloat = (kScreenW - 20 - itemMargin * 2) / 3
class CZStatusPictureView: UICollectionView {
    
    
    // 
    var pictureInfo: [CZPictureInfo]?{
        didSet{
            countLabel.text = "\(pictureInfo?.count ?? 0)"
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            
            // 更改自己的约束 没有涉及到被人来约束的 需要使用update 更简单
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            // 得到数据后 记得刷新
            self.reloadData()
            
        }
    }
    
    // 处理我们的配图size
    func getPictureViewSize(count: Int) -> CGSize{
        
        
        // 列数 和行数
        let col = count >= 3 ? 3 : count
        let row = (count - 1) / 3 + 1
        
        // 配图的宽高
        let width = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        return CGSize(width: width, height: height)
    }
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 由于该控件原创微博和转发微博都需要使用 所以需要设置成透明色
        self.backgroundColor = UIColor.clearColor()
        // 设置数据源方法
        dataSource = self
        
        // 注册cell
        registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: CZStatusPictureViewCellId)
        
        // 设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        // 01 添加控件
        addSubview(countLabel)
        // 02 添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    
    // MARK: - 懒加载控件
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 34)

}

extension CZStatusPictureView: UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZStatusPictureViewCellId, forIndexPath: indexPath) as! CZStatusPictureViewCell
        
        // 给自定义cell赋值
        cell.pic = pictureInfo![indexPath.item]
        return cell
    }
}

// 自定义的配图cell
class CZStatusPictureViewCell: UICollectionViewCell {
    
    var pic: CZPictureInfo?{
        didSet{
            bgImageView.sd_setImageWithURL(NSURL(string: pic?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            
            /*
                - 是否为.gif 图片
                - url 后缀为.gif === 动态图片
                - url 后缀不为.git === 正常图片
            
            */
            // 动态图片
            if let p = pic?.thumbnail_pic where p.hasSuffix(".gif") {
                gifImageView.hidden = false
            }else{
                // 不是动态图片
                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        
        self.backgroundColor = UIColor.whiteColor()
        
        // 01- 添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        // 02- 添加约束
        // 01
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        // 02
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.bottom.equalTo(contentView)
        }
    }
    
    // MARK: - 懒加载控件
    
    // 背景配图图片
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "timeline_card_top_background"))
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    // 动态图片logo
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
}









